#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Process                                                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 11/25/2016                                             #
# Last Modification: 16/10/2017                                             #
# Author...........: focus-em.org                                           #
#                                                                           #
#############################################################################
#
# SORTORDER: 60
#
# MANUAL: This script is under development... 
#
# DISPLAY: imagenumber
# DISPLAY: comment
# DISPLAY: sample_pixel
# DISPLAY: gautomatch_diameter
# DISPLAY: gautomatch_cc_cutoff
# DISPLAY: gautomatch_extractraw
# DISPLAY: gautomatch_boxsize
# DISPLAY: gautomatch_mindist
# DISPLAY: KV
# DISPLAY: CS
# DISPLAY: gautomatch_number_particles
# DISPLAY: gautomatch_extra
# DISPLAY: multi_series_number_first
# DISPLAY: multi_series_number_last
# DISPLAY: multi_defocus
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_mrc_converter = ""
#
set tempkeep = ""
set imagename = ""
set nonmaskimagename = ""
set stepdigitizer = ""
set Calc_from_sample_pixel = ""
set sample_pixel = ""
set phacon = ""
set RESMIN = ""
set RESMAX = ""
set RADLIM = ""
set KV = ""
set CS = ""
set movie_stackname = ""
set import_original_time = ""
set gautomatch_diameter = ""
set gautomatch_cc_cutoff = ""
set gautomatch_number_particles = ""
set gautomatch_extra = ""
set gautomatch_extractraw = ""
set gautomatch_boxsize = ""
set gautomatch_mindist = ""
set multi_series_number_first = ""
set multi_series_number_last = ""
set multi_defocus = ""
#
#$end_vars
#
set scriptname = focpair_process
\rm -f LOGS/${scriptname}.results
#
source ${proc_2dx}/initialize
#
echo "<<@progress: 5>>"
#
if(${import_original_time} == "-" || ${import_original_time} == "") then
  @ status_date = `date +%s` * 1000
  set date_text = "Processed at "`echo ${status_date} | awk ' { s = $1 / 1000 } END { print s } ' | ${app_gawk} '{print strftime("%c", $0)}' `
else
  set status_date = ${import_original_time}
  set date_text = "Recorded at "`echo ${status_date} | awk ' { s = $1 / 1000 } END { print s } ' | ${app_gawk} '{print strftime("%c", $0)}' `
endif
#
echo "# IMAGE: ${movie_stackname}_Sum_fft.mrc <DriftCor image FFT (2D, no DW)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${movie_stackname}.mrc <DriftCor image (2D, with DW)>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${movie_stackname}_fft.mrc <DriftCor image FFT (2D, with DW)>" >> LOGS/${scriptname}.results
#
if ( ${GPU_how_many} > "1" ) then
  if ( ${GPU_cycle} == "y" ) then
    source ${proc_2dx}/2dx_find_GPU.com
    set GPU_to_use_local = ${next_GPU}
  else
    set GPU_to_use_local = 0
  endif
endif
#
if ( ${CS} == "ScriptWillPutNumbereHere" ) then
  set CS = ${Default_CS}
  echo "set CS = ${CS}" >> LOGS/${scriptname}.results
endif
#
if ( ${KV} == "ScriptWillPutNumbereHere" ) then
  set KV = ${Default_KV}
  echo "set KV = ${KV}" >> LOGS/${scriptname}.results
endif
#
echo "<<@progress: 10>>"
echo "<<@evaluate>>"
#
set input_image = ${movie_stackname}
if ( -e ${movie_stackname}_0_Sum.mrc ) then
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_0_Sum.mrc <DriftCor image 0 (2D, no DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_1_Sum.mrc <DriftCor image 1 (2D, no DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_0_Sum_fft.mrc <DriftCor image FFT 0 (2D, no DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_1_Sum_fft.mrc <DriftCor image FFT 1 (2D, no DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE: ${movie_stackname}_0.mrc <DriftCor image 0 (2D, with DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE: ${movie_stackname}_1.mrc <DriftCor image 1 (2D, with DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE: ${movie_stackname}_0_fft.mrc <DriftCor image FFT 0 (2D, with DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE: ${movie_stackname}_1_fft.mrc <DriftCor image FFT 1 (2D, with DW)>" >> LOGS/${scriptname}.results
else
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_0.mrc <DriftCor image 0 (2D, with DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_1.mrc <DriftCor image 1 (2D, with DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_0_fft.mrc <DriftCor image FFT 0 (2D, with DW)>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: ${movie_stackname}_1_fft.mrc <DriftCor image FFT 1 (2D, with DW)>" >> LOGS/${scriptname}.results
endif
#
echo "# IMAGE: ${movie_stackname}_0_pf.mrc <Phase-flipped average 0 (2D)>" >> LOGS/${scriptname}.results
echo "# IMAGE: ${movie_stackname}_1_pf.mrc <Phase-flipped average 1 (2D)>" >> LOGS/${scriptname}.results
#
##########################################################################
${proc_2dx}/linblock "Phase flipping files"
##########################################################################
#
set current_filenumber = ${multi_series_number_first}
#
while ( ${current_filenumber} <= ${multi_series_number_last} )
  #
  set current_filename = ${input_image}_${current_filenumber}
  #
  ##########################################################################
  ########################################################################## 
  #

  #
  @ current_filenumber += 1
end
#
##########################################################################
${proc_2dx}/linblock "Transforming the average images into stacks"
##########################################################################
#
echo "# IMAGE-IMPORTANT: ${movie_stackname}_pf.mrcs <Phase-flipped movie (2 frames)>" >> LOGS/${scriptname}.results
newstack -in ${movie_stackname}_0_pf.mrc -in ${movie_stackname}_1_pf.mrc -ou ${movie_stackname}_pf.mrcs
#
##########################################################################
${proc_2dx}/linblock "Aligning the second onto the first file"
##########################################################################
#
echo "# IMAGE-IMPORTANT: ${movie_stackname}_pf.xf <Transform file>" >> LOGS/${scriptname}.results
# xfalign -InputImageFile ${movie_stackname}_pf.mrcs -OutputTransformFile ${movie_stackname}_pf.xf -filter 0.02,0.05,0,0.1 -params 3 -bilinear -prexcorr -local 256 -warp 2000,2000
#
echo "# IMAGE-IMPORTANT: ${movie_stackname}_pf_motioncor2Sum.mrc <MC2 sum>" >> LOGS/${scriptname}.results
#
time ${app_motioncor2} \
-InMrc ${movie_stackname}_pf.mrcs \
-OutMrc ${movie_stackname}_pf_motioncor2Sum.mrc \
-Iter 15 \
-Tol 0.4 \
-kV ${KV} \
-bft 200 150 \
-PixSize ${sample_pixel} \
-Gpu ${GPU_to_use_local}   \
 > LOGS/motioncor2.out
#
echo "<<@progress: 100>>"
echo "<<@evaluate>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
exit
#
